<template>
    <div class="cart">
        <div class="container">
            <ul class="cartProcess">
                <li>1.购物车</li>
                <li>2.填写订单</li>
                <li>3.成功提交</li>
            </ul>
            <div class="cartMenu" v-show="isCar">
                <el-table
                    ref="cartList"
                    :data="cartList"
                    tooltip-effect="dark"
                    style="width: 100%"
                    @selection-change="handleSelectionChange"
                >
                    <el-table-column
                        type="selection"
                        width="28"
                        label="全选"
                        align="center"
                    ></el-table-column>

                    <!-- 商品图片 -->
                    <el-table-column
                        prop="imgUrl"
                        label=""
                        width="172"
                        align="center"
                    >
                        <template slot-scope="scope"
                            ><img
                                :src="scope.row.s_good.s_goods_photos[0].path"
                                style="width: 70px"
                                alt=""
                        /></template>
                    </el-table-column>

                    <!-- 商品名称 -->
                    <el-table-column
                        prop="name"
                        label="商品"
                        width="400"
                        align="left"
                    >
                        <template slot-scope="scope">{{
                            scope.row.s_good.name
                        }}</template>
                    </el-table-column>

                    <!-- 商品价格 -->
                    <el-table-column
                        prop="price"
                        label="价格"
                        width="132"
                        align="left"
                        class="price"
                    >
                        <template slot-scope="scope">
                            ￥{{ scope.row.s_good.price }}
                        </template>
                    </el-table-column>

                    <!-- 商品数量 -->
                    <el-table-column
                        prop="number"
                        label="数量"
                        width="222"
                        align="left"
                        size="small"
                    >
                        <template slot-scope="scope">
                            <el-input-number
                                v-model="scope.row.num"
                                controls-position="right"
                                @change="
                                    handleChange(scope.row.id, scope.row.num)
                                "
                                :min="1"
                                :max="100"
                            ></el-input-number>
                        </template>
                    </el-table-column>

                    <!-- 商品小计 -->
                    <el-table-column
                        label="总计"
                        width="150"
                        align="left"
                        class="totalPrice"
                    >
                        <template slot-scope="scope">
                            ￥{{ scope.row.num * scope.row.s_good.price }}
                        </template>
                    </el-table-column>

                    <!-- 删除操作 -->
                    <el-table-column fixed="right" label="操作">
                        <template slot-scope="scope">
                            <el-popconfirm
                                title="确定删除吗？"
                                @confirm="deleteRow(scope.row.id)"
                                placement="top"
                            >
                                <el-button
                                    type="text"
                                    size="small"
                                    slot="reference"
                                    ><i class="el-icon-delete"></i
                                ></el-button>
                            </el-popconfirm>
                        </template>
                    </el-table-column>
                </el-table>

                <!-- 商品数量小计信息 -->
                <div class="feeMenu" style="margin-top: 20px">
                    <p class="feeNumber">
                        共 <span>{{ cartList.length }}</span> 种商品，已选择
                        <span>{{ goodsNumber }}</span> 件
                    </p>
                    <p class="feeTotal">
                        总计（不含运费）：<span>￥{{ goodsTotal }}</span>
                    </p>
                </div>

                <div class="freight">
                    <img
                        src="https://www.tp-linkshop.com.cn/Content/themes/base/images/Product/freight.png"
                        alt=""
                    />
                </div>
                <div class="clear"></div>
                <!-- 结算按钮 -->
                <div class="procedure">
                    <p>
                        若要使用企业优惠，请先成功提交订单后，付款之前联系客服改价。
                    </p>
                    <div class="buyBtn">
                        <div class="back">
                            <router-link to="/">&lt;继续购物</router-link>
                        </div>
                        <div class="topay" @click="pay">去结算</div>
                    </div>
                </div>
            </div>
            <div class="noCart" v-show="!isCar">
                <p>您的购物车内没有商品，请先挑选商品哦</p>
                <div class="back">
                    <router-link to="/">选购商品</router-link>
                </div>
            </div>
        </div>
    </div>
</template>

<script>
import axios from "axios";

export default {
    data() {
        return {
            multipleSelection: [],
            // 判断购物车内有无商品，如果有则显示cartMenu，如果无则显示noCart
            isCar: true,
            // 获取的购物车详情列表
            cartList: [],
        };
    },
    computed: {
        // 选择之后的商品数量
        goodsNumber() {
            if (this.multipleSelection.length) {
                // 累加
                return this.multipleSelection.reduce((total, currentItem) => {
                    return (total += currentItem.num);
                }, 0);
            } else {
                return 0;
            }
        },
        // 选中之后的商品总价
        goodsTotal() {
            if (this.multipleSelection.length) {
                // 累加
                return this.multipleSelection.reduce((total, currentItem) => {
                    return (total +=
                        currentItem.num * currentItem.s_good.price);
                }, 0);
            } else {
                return 0;
            }
        },
    },
    created() {
        // 获取购物车信息
        axios({
            method: "GET",
            url: "http://shop.bluej.cn/api/shoppingCart",
            params: {
                project_id: 90,
            },
            headers: {
                "x-token": this.$store.state.token,
            },
        })
            .then((res) => {
                this.cartList = res.data.result;
                if (this.cartList.length) {
                    // 判断获取的商品有无，有就显示正常购物车页，否则显示提示页
                    this.isCar = true;
                } else {
                    this.isCar = false;
                }
            })
            .then((err) => {
                console.log(err);
            });
    },
    methods: {
        // 点击勾选
        handleSelectionChange(val) {
            this.multipleSelection = val;
            console.log(this.multipleSelection);
        },
        // 删除功能
        deleteRow(id) {
            axios({
                method: "DELETE",
                url: `http://shop.bluej.cn/api/shoppingCart/${id}`,
                headers: {
                    "x-token": this.$store.state.token,
                },
            })
                .then((res) => {
                    // 删除刷新本页面
                    this.$router.go(0);
                })
                .then((err) => {
                    console.log(err);
                });
        },
        // 修改某条购物车数量
        handleChange(id, num) {
            axios({
                method: "PUT",
                url: `http://shop.bluej.cn/api/shoppingCart/${id}`,
                data: {
                    project_id: 90,
                    num: num,
                },
                headers: {
                    "x-token": this.$store.state.token,
                },
            })
                .then((res) => {
                    console.log(res);
                })
                .then((err) => {
                    console.log(err);
                });
        },
        // 下单功能
        pay() {
            if (this.goodsNumber > 0) {
                // 将勾选商品传参给check页
                let str = JSON.stringify(this.multipleSelection);
                this.$router.push({ path: "/shopping/check", query: { str } });
            } else {
                alert("结算前请先勾选中商品");
            }
        },
    },
};
</script>

<style lang="scss" scoped>
.cart {
    width: 100%;
    min-width: 1200px;
    .container {
        width: 1200px;
        margin: 0 auto;
        .cartProcess {
            width: 100%;
            height: 60px;
            li {
                display: flex;
                justify-content: center;
                align-items: center;
                float: left;
                width: 400px;
                height: 60px;
                border-bottom: 1px solid #e6e6e6;
                color: #333;
                font-size: 18px;
                &:first-child {
                    color: skyblue;
                    border-color: skyblue;
                }
            }
        }
        .cartMenu {
            ::v-deep .el-table_1_column_4 {
                color: red;
            }
            ::v-deep .el-table_1_column_6 {
                color: red;
            }
            .feeMenu {
                width: 100%;
                margin-top: 20px;
                display: flex;
                justify-content: space-between;
                align-items: center;
                .feeNumber {
                    font-size: 14px;
                    color: #333;
                    line-height: 19px;
                    span {
                        color: #0096ff;
                    }
                }
                .feeTotal {
                    font-size: 18px;
                    color: #333;
                    line-height: 24px;
                    span {
                        font-size: 18px;
                        color: #ff3232;
                        line-height: 24px;
                    }
                }
            }
            .freight {
                margin: 16px 0;
                float: right;
            }
            .clear {
                clear: both;
            }
            .procedure {
                width: 100%;
                padding-top: 20px;
                display: flex;
                justify-content: space-between;
                align-items: center;
                border-top: 1px solid #e6e6e6;
                p {
                    font-size: 14px;
                    color: #333;
                    line-height: 20px;
                }
                .buyBtn {
                    display: flex;
                    justify-content: space-between;
                    align-items: center;
                    .back,
                    .topay {
                        border: 1px solid #ff3232;
                        margin-left: 16px;
                        border-radius: 2px;
                        width: 130px;
                        height: 40px;
                        line-height: 40px;
                        text-align: center;
                        cursor: pointer;
                        font-size: 16px;
                        display: inline-block;
                    }
                    .back {
                        a {
                            color: #ff3232;
                        }
                    }
                    .topay {
                        background: #ff3232;
                        color: #fff;
                    }
                }
            }
        }
        .noCart {
            p {
                font-size: 18px;
                padding: 50px 0 30px;
                color: #333;
            }
            .back {
                margin-left: 16px;
                border-radius: 2px;
                width: 130px;
                height: 40px;
                line-height: 40px;
                text-align: center;
                cursor: pointer;
                font-size: 16px;
                display: inline-block;
                background: #ff3232;
                a {
                    color: #fff;
                }
            }
        }
    }
}
</style>
